import { getToken } from '@/utils/auth'

// 响应拦截器
uni.addInterceptor({
  returnValue (res) {
    if (!(!!res && (typeof res === "object" || typeof res === "function") && typeof res.then === "function")) {
      return res;
    }
    return new Promise((resolve, reject) => {
      res.then((res: [unknown, unknown]) => res[0] ? reject(res[0]) : resolve(res[1]));
    });
  },
});

// 为请求设置请求拦截器添加用户token信息
uni.addInterceptor('request', {
  invoke: (options) => { 
    
    if (!options.header) { 
      options.header = {}
    }
    const token = getToken() || ''
    if (token) { 
      options.header.Authorization = "Bearer " + token
    }
    options.header.Accept = 'application/json;charset=UTF-8'
    return options
  }
})
